package com.liuyz;

public class Cat implements Runnable {

    String name;

    int age;

    String color;

    // 统计这个类的所有对象一共被使用了多少次
    static int num = 0;

    public Cat(int age, String color) {
        this.age = age;
        this.color = color;
    }

    public Cat(String name, int age, String color) {
        // this(实参列表)、只能在构造器中使用、必须放在第一条语句执行
        this(age, color);
        this.name = name;
    }


    void fun1() throws InterruptedException {
        synchronized (this) {
            System.out.println(++num);
            System.out.println("begin_" + Thread.currentThread().getName() + "_" + System.currentTimeMillis());
            System.out.println(this.name + "_" + age + "_" + color + "_" + this.hashCode());
            Thread.sleep(1000);
            System.out.println("end_" + Thread.currentThread().getName() + "_" + System.currentTimeMillis());
        }
    }

    @Override
    public void run() {
       /* try {
            this.fun1();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/

        Cat cat = new Cat(Thread.currentThread().getName(), 1, "1");
        System.out.println(cat + "_" + cat.hashCode());
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }
}
